package com.weds.edpf.core.service;

import com.weds.core.base.BaseService;
import com.weds.edpf.core.entity.DepEntity;
import com.weds.edpf.core.entity.UserBaseEntity;
import com.weds.edpf.core.mapper.DepMapper;
import com.weds.edpf.core.util.CharacterUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author sxm
 * @Description Dep管理
 * @Date 2018-12-13
 */
@Service
public class DepService extends BaseService {

    @Autowired
    private DepMapper depMapper;

    @Autowired
    UserBaseService userBaseService;

    private Logger log = LogManager.getLogger();

    public int deleteByPrimaryKey(Long depSerial) {
        return depMapper.deleteByPrimaryKey(depSerial);
    }

    public int insert(DepEntity record) {
        return depMapper.insert(record);
    }

    public int insertSelective(DepEntity record) {
        return depMapper.insertSelective(record);
    }

    public DepEntity selectByPrimaryKey(Long depSerial) {
        return depMapper.selectByPrimaryKey(depSerial);
    }

    public int updateByPrimaryKeySelective(DepEntity record) {
        return depMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(DepEntity record) {
        return depMapper.updateByPrimaryKey(record);
    }

    public List<DepEntity> selectListByEntity(DepEntity record) {
        return depMapper.selectListByEntity(record);
    }

    public List<DepEntity> selectDeptUserNums(Integer depParentNo) {
        return depMapper.selectDeptUserNums(depParentNo);
    }

    public List<DepEntity> selectOrgTree(Integer depParentNo) {
        return depMapper.selectOrgTree(depParentNo);
    }

    public List<DepEntity> selectOrgTreeLazy(Integer depParentNo) {
        return depMapper.selectOrgTreeLazy(depParentNo);
    }
    public int countDepByParent(Integer depParentNo) {
        return depMapper.countDepByParent(depParentNo);
    }

    public DepEntity getRootDir() {
        return depMapper.getRootDir();
    }

    public List<DepEntity> selectListMorethanOrder(Integer depParentNo,Integer depOrder) {
        return depMapper.selectListMorethanOrder( depParentNo, depOrder);
    }
    public int updateDepNoPrimaryKey(DepEntity record) {
        return depMapper.updateDepNoPrimaryKey(record);
    }

    public List<DepEntity> selectListByDepParent(Integer depParentNo) {
        return depMapper.selectListByDepParent( depParentNo);
    }

    public List<DepEntity> selectListBetwweenOrder(Integer depParentNo, Integer order1, Integer order2) {
        return depMapper.selectListBetwweenOrder(depParentNo, order1, order2);
    }

    /**
     * 处理同级部门及子部门顺序变更 更新部门下用户detNo
     * @param list2
     * @param depSerial
     * @param upDown
     */
    public void depReOrder( List<DepEntity> list2 ,Long depSerial ,int upDown){
        if(list2 != null && list2.size() != 0){
            for (DepEntity depEntity : list2) {
                if(depEntity.getDepSerial().equals(depSerial)){
                    continue;
                }
                DepEntity entity1 = new DepEntity();
                int newDepOrder = depEntity.getDepOrder()+upDown;
                entity1.setDepOrder(newDepOrder);
                String d = CharacterUtil.DeciamlToThirtySix(newDepOrder);
                if(d.length() == 1){
                    d = "0" + d;
                }
                String  depNo = depEntity.getDepNo();
                String newDepNo = depNo.substring(0,depNo.length() -2) + d;
                entity1.setDepNo(newDepNo);
                entity1.setDepSerial(depEntity.getDepSerial());
                //同级相关部门 更新
                this.updateDepNoPrimaryKey(entity1);

                //再更新dtuser里的depNo
                UserBaseEntity userBaseEntity = new UserBaseEntity();
                userBaseEntity.setDepNo(newDepNo);
                userBaseEntity.setUserDep(depEntity.getDepSerial().intValue());
                userBaseService.updateDepNo(userBaseEntity);

                //子目录相应变更
                childDepUserUpdate(depEntity.getDepSerial(),newDepNo,0);
            }
        }

    }

    /**
     * 子目录 depNo修改   dtuser depNo修改
     * @param depSerial
     * @param newDepNo
     * @param index
     */
    public void childDepUserUpdate(Long depSerial, String newDepNo,int index){
        if(index > 30){
            return ;
        }
        //子目录相应变更
        List<DepEntity>  childDep = this.selectListByDepParent(depSerial.intValue());
        if(childDep != null && childDep.size() != 0){
            index++;
            for (DepEntity entity : childDep) {
                DepEntity entity2 = new DepEntity();
                String order = entity.getDepNo();
                if(order.length() == 1){
                    order = "0"+order;
                }
                entity2.setDepNo(newDepNo + order);
                entity2.setDepSerial(entity.getDepSerial());
                this.updateByPrimaryKeySelective(entity);

                //再更新dtuser里的depNo
                UserBaseEntity entity3 = new UserBaseEntity();
                entity3.setDepNo(entity2.getDepNo());
                entity3.setUserDep(entity2.getDepSerial().intValue());
                userBaseService.updateDepNo(entity3);

                childDepUserUpdate(entity.getDepSerial(),entity2.getDepNo(),index);
            }
        }

    }


}

